'use strict';

define(['controllers', '../service/menuService'], function(controllers) {
    controllers.common.controller('BreadCrumbController', ['$scope', '$location', 'MenuService', function ($scope, $location, menuService) {
        function locationChange() {
            var menuIdMap = menuService.getMenuIdMap();
            var menuUrlMap = menuService.getMenuUrlMap();

            var menus = [];
            var path = $location.path();
            var menuUrl = menuService.getOriginMenuUrl(path);
            var menu = menuUrlMap[menuUrl];
            while (menu != null) {
                menus.unshift(menu);
                menu = menu.parentId != null ? menuIdMap[menu.parentId] : null;
            }
            $scope.menus = menus;
        }

        locationChange();
        $scope.$on('$locationChangeSuccess', function(event, path) {
            locationChange();
        });
    }]);
});